package org.layui.shop.controller;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.layui.shop.dao.ProductDao;
import org.layui.shop.dao.UserDao;
import org.layui.shop.dao.impl.ProductDaoImpl;
import org.layui.shop.dao.impl.UserDaoImpl;
import org.layui.shop.entity.Product;
import org.layui.shop.entity.User;
import org.layui.shop.util.Md5Util;

import java.io.IOException;

/**
 * 修改商品信息
 *
 * @author ZQX
 * @date 2024-12-03
 */
@WebServlet("/update_product.do")
public class UpdateProductServlet extends BaseController {
    @Override
    public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");

        // 1.获取表单中输入的数据
        String productId = req.getParameter("productId");
        String productName = req.getParameter("productName");
        String productType = req.getParameter("productType");
        double productPrice = Double.parseDouble(req.getParameter("productPrice")) ;
        int productStatus = Integer.parseInt(req.getParameter("productStatus")) ;
        String productDesc = req.getParameter("productDesc");

        // 2.验证数据的合法性 - 正则表达式（略）
        Product p = new Product();
        p.setProductId(productId);
        p.setProductName(productName);
        p.setProductType(productType);
        p.setProductPrice(productPrice);
        p.setProductStatus(productStatus);
        p.setProductDesc(productDesc);


        // 3.加工处理 - 业务实现
        // 3.1）实例化 DAO 对象
        ProductDao productDao = new ProductDaoImpl();

        // 3.2）调用 DAO 对象的方法，实现商品信息的修改
        int row = productDao.update(p);

        // 4.响应结果 - 先数据封装到 ResponseData,然后序列为 JSON 字符串，最后响应数据
        if(row==1) {
            print(res,success(200,null,"修改成功"));
            return;
        }
        print(res,error("修改失败"));
    }
}
